package com.quzhi.sys.service.impl;

import com.quzhi.sys.dto.GoodsRecordDto;
import com.quzhi.sys.dto.ReceiveGoodsDto;
import com.quzhi.sys.entity.GoodsSerialNumber;
import com.quzhi.sys.entity.ReceiveGoods;
import com.quzhi.sys.mapper.ReceiveGoodsMapper;
import com.quzhi.sys.service.IReceiveGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Xibing
 * @since 2024-07-09
 */
@Service
public class ReceiveGoodsServiceImpl extends ServiceImpl<ReceiveGoodsMapper, ReceiveGoods> implements IReceiveGoodsService {


    // 更具序列号获取运动
    @Override
    public List<ReceiveGoodsDto> getNeedGoodsRecordDtoList(List<GoodsSerialNumber> goodsSerialNumberList) {
        // 获取记录
        List<ReceiveGoodsDto> receiveGoodsDtos = new ArrayList<>();

        // 用以下这套逻辑查询效率更高
        for (GoodsSerialNumber goodsSerialNumber : goodsSerialNumberList) {

            // 获取订单
            if (receiveGoodsDtos.size() < 1) {
                // 没有就添加
                ReceiveGoodsDto receiveGoodsDto = new ReceiveGoodsDto();

                // 查询订单
                ReceiveGoods receiveGoods = this.baseMapper.selectById(goodsSerialNumber.getReceiveGoodsId());

                BeanUtils.copyProperties(receiveGoods, receiveGoodsDto);

                // 将序列号添加进去
                List<GoodsSerialNumber> goodsSerialNumbers = receiveGoodsDto.getGoodsSerialNumbers();
                goodsSerialNumbers.add(goodsSerialNumber);
                receiveGoodsDto.setGoodsSerialNumbers(goodsSerialNumbers);

                // 查询订单表
                receiveGoodsDtos.add(receiveGoodsDto);
            } else {
                // 查询收货记录

                // 判断列表中是否有就添加序列号对应的收货表
                boolean flag = true;
                for (ReceiveGoodsDto receiveGoodsDto : receiveGoodsDtos) {
                    if (receiveGoodsDto.getId().equals(goodsSerialNumber.getReceiveGoodsId())) {
                        // 有相同的直接添加
                        List<GoodsSerialNumber> goodsSerialNumbers = receiveGoodsDto.getGoodsSerialNumbers();
                        goodsSerialNumbers.add(goodsSerialNumber);
                        receiveGoodsDto.setGoodsSerialNumbers(goodsSerialNumbers);
                        flag = false;
                    }
                }

                // 没有就查询订单记录
                if (flag) {
                    // 没有就添加
                    ReceiveGoodsDto receiveGoodsDto = new ReceiveGoodsDto();

                    // 查询订单
                    ReceiveGoods receiveGoods = this.baseMapper.selectById(goodsSerialNumber.getReceiveGoodsId());

                    if(receiveGoods != null){
                        BeanUtils.copyProperties(receiveGoods, receiveGoodsDto);
                    }

                    // 将序列号添加进去
                    List<GoodsSerialNumber> goodsSerialNumbers = receiveGoodsDto.getGoodsSerialNumbers();
                    goodsSerialNumbers.add(goodsSerialNumber);
                    receiveGoodsDto.setGoodsSerialNumbers(goodsSerialNumbers);

                    // 查询订单表
                    receiveGoodsDtos.add(receiveGoodsDto);
                }

            }


        }

        return receiveGoodsDtos;
    }
}
